﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SearchForString
{
    class Program
    {
        const string ResultFilename = @"C:\result.txt";

        static void SearchString(string stringToSearch, string folderPath , StreamWriter outputFile)
        {
            string[] files = Directory.GetFiles(folderPath);
            string[] folders = Directory.GetDirectories(folderPath);

            foreach (var file in files)
            {
                StreamReader inputFile = new StreamReader(file);
                using (inputFile)
                {
                    string line = inputFile.ReadLine();
                    while (line != null)
                    {
                        if (line.IndexOf(stringToSearch)!=-1)
                        {
                            outputFile.WriteLine(file);
                            outputFile.WriteLine(line);
                            outputFile.WriteLine();
                            break;
                        }
                        line = inputFile.ReadLine();
                    }
                }
            }

            foreach (var folder in folders)
            {
                SearchString(stringToSearch, folder, outputFile);
            }
        }

        static void Main(string[] args)
        {
            StreamWriter outputFile = new StreamWriter(ResultFilename);
            using (outputFile)
            {
                SearchString("pesho", @"D:\CodeplexRepository\Panayot Popov\InputOutput\InputOutput\SearchForString\pesho", outputFile);
            }
        }
    }
}
